package cn.newgrand.common;

import org.jivesoftware.Spark;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.util.log.Log;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-9-19 下午04:56:26
 * 
 * 定义netcall://协议调用netcall.exe的参数
 * 
 */
public class URLMappingUtil {
	
	public static final String STANDARD_HEADER ="netcall://";
	public static final String LOGIN_HEADER ="login/?";
	public static final String SEND_MESSAGE_HEADER ="message/?";
	public static final String JOIN_ROOM_HEADER ="join/?";
	public static final String JOIN_EXPERT_QUEUE ="consultant/?";
	public static final String LOGIN_USER_NAME ="login_username";
	public static final String LOGIN_PASSWORD ="login_password";
	public static final String LOGIN_SERVER ="login_server";
	public static final String MESSAGE_TO_JID ="message_to_jid";
	public static final String MESSAGE_BODY ="message_body";
	public static final String JOIN_ROOM_JID ="join_room_jid";
	public static final String CONSULTANT_SPID = "spid";
	
	public static boolean isStandardProtocol(String url){
//		org.jivesoftware.spark.util.log.Log.error("url mapping "+(url.indexOf(STANDARD_HEADER)!=-1));
		return url.indexOf(STANDARD_HEADER)!=-1;
	}
	public static boolean isToLogin(String url){
		if(!isStandardProtocol(url)) return false;
		return url.indexOf(LOGIN_HEADER)!=-1;
	}
	public static boolean isToSendMessage(String url){
//		org.jivesoftware.spark.util.log.Log.error("url mapping "+(url.indexOf(SEND_MESSAGE_HEADER)!=-1));
		if(!isStandardProtocol(url)) return false;
		
		return url.indexOf(SEND_MESSAGE_HEADER)!=-1;
	}
	public static boolean isToConsultant(String url){
		
		if(!isStandardProtocol(url)) return false;
		return url.indexOf(JOIN_EXPERT_QUEUE)!=-1;
	}
	public static boolean isToJoinRoom(String url){
		if(!isStandardProtocol(url)) return false;
		return url.indexOf(JOIN_ROOM_HEADER)!=-1;
	}
	
	public static String getMessageToJID(String url){
		if(!isToSendMessage(url)) return null;
		String temp = getParameter(url,MESSAGE_TO_JID);
		if(StringUtil.isEmpty(temp)){
			return null;
		}
		if(temp.indexOf("@")==-1){
			return temp+"@"+SparkManager.getConnection().getServiceName();
		}
		return temp;
	}
	public static String getMessageBody(String url){
		if(!isToSendMessage(url)) return null;
		return getParameter(url,MESSAGE_BODY);
	}
	public static String getLoginUsername(String url){
		if(!isToLogin(url)) return null;
		return getParameter(url,LOGIN_USER_NAME);
	}
	public static String getLoginPassword(String url){
		if(!isToLogin(url)) return null;
		String encodedPassword = getParameter(url,LOGIN_PASSWORD);
		return StringUtil.decodeStringNG(encodedPassword, 128);
	}
	public static String getLoginServer(String url){
		if(!isToLogin(url)) return null;
		return getParameter(url,LOGIN_SERVER);
	}
	public static String getJoinRoomJID(String url){
		if(!isToJoinRoom(url)) return null;
//		Log.error("conference: "+getParameter(url,JOIN_ROOM_JID));
		String temp = getParameter(url,JOIN_ROOM_JID);
		if(StringUtil.isEmpty(temp)){
			return null;
		}
		if(temp.indexOf("@")==-1){
			return temp+"@"+SparkManager.getConnection().getServiceName();
		}
		
		return temp;
	}
	
	public static String getConsultantSpid(String url){
		if(!isToConsultant(url)) return null;
		return getParameter(url,CONSULTANT_SPID);
	}
	
	
	public static String getParameter(String url,String argName){
		if (url == null) {
            return null;
        }
        String arg = argName + "=";

        int index = url.indexOf(arg);
        if (index == -1) {
            return null;
        }

        String value = url.substring(index + arg.length());
//        int index2 = value.indexOf("&");
        //URL参数分隔符采用@@**的形式
        int index2 = value.indexOf("@@**");
        if (index2 != -1) {
            // Must be the last argument
            value = value.substring(0, index2);
        }
        
        int index3 = value.indexOf("/");
        if(index3!=-1){
        	value = value.substring(0,index3);
        }
        
        int index4 = value.indexOf("\\");
        if(index4!=-1){
        	value = value.substring(0,index4);
        }

        return value;
	}
	
	public static void main(String[] args){
		System.out.println(StringUtil.EncodeStringNG("123456", 128));
	}
	
}
